package com.iflytek.cosmo.ocr.web.controller;

import java.util.Arrays;
import java.util.List;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.iflytek.cosmo.ocr.common.config.IflyBarAuthProperties;
import com.iflytek.cosmo.ocr.common.constant.CommonConstant;
import com.iflytek.cosmo.ocr.dao.IflyBarAuthDao;
import com.iflytek.cosmo.ocr.dao.MacLicenseDao;
import com.iflytek.cosmo.ocr.model.domain.MessageOutput;
import com.iflytek.cosmo.ocr.model.domain.State;
import com.iflytek.cosmo.ocr.model.entity.LicenseEntity;
import com.iflytek.cosmo.ocr.model.entity.MacLicenseEntity;
import com.iflytek.cosmo.ocr.service.AuthorizationService;
import com.iflytek.cosmo.ocr.web.vo.AuthrizationVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.collections4.CollectionUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import static com.iflytek.cosmo.ocr.common.enums.LicenseEnum.TRIAL;

/**
 * @author ViviZhou
 * @create 2019-12-12 16:38
 */
@RestController
@Api(value = "授权模块控制层", tags = "授权模块控制层")
@RequestMapping(value = "/authorization")
public class AuthorizationController {

    private Logger logger = LoggerFactory.getLogger(AuthorizationController.class);

    @Autowired
    AuthorizationService authorizationService;

    @Autowired
    IflyBarAuthProperties iflyBarAuthProperties;

    @Autowired
    IflyBarAuthDao iflyBarAuthDao;

    @Autowired
    MacLicenseDao macLicenseDao;

    // @Value("${license.validateDateMap.trial}")
    // private int trialTimeDay;
    //
    // @Value("${license.validateDateMap.formal}")
    // private int formalTimeMonth;
    //
    // @Value("${license.validateDateMap.free}")
    // private int freeTimeYear;

    @RequestMapping(value = "/search", method = {RequestMethod.POST})
    @ApiOperation(value = "授权用户信息查询", notes = "根据mac或者主板号，查询当前客户端是否授权")
    public MessageOutput searchAuthorization(@RequestBody String body){
        //接受json形式参数
        JSONObject bodyJson = JSON.parseObject(body);
        JSONArray macIds = bodyJson.getJSONArray("mac");
        String[] macIdArray = macIds.toArray(new String[macIds.size()]);
        List<String> motherBoardList = Arrays.asList(macIdArray);

        AuthrizationVO authrizationVO = new AuthrizationVO();
        // 根据主板号查询MacLicenseEntity，获取绑定授权码的id
        List<MacLicenseEntity> macLicenseEntities = authorizationService.findLicenseId(motherBoardList);
        // 根据其中一个mac的codeid查询license
        LicenseEntity licenseEntity = null;
        if (CollectionUtils.isNotEmpty(macLicenseEntities)){
            licenseEntity = authorizationService.findLicenseEntityById(macLicenseEntities.get(0).getCodeId());
        }
        // if (iflyBarAuthProperties.getAuthEnable() == true){
        //     //免授权模式处理方式
        //     return authorizationService.freeAuthorizationModel(macIdArray, authrizationVO, motherBoardList, licenseEntity);
        // }else {
            // 非免授权，根据licenseEntity，查看是否有授权码，是否过期
            return authorizationService.noneFreeAuthorizationModel(licenseEntity, authrizationVO);
        // }
    }


    // @RequestMapping(value = "/trail", method = {RequestMethod.POST})
    // @ApiOperation(value = "调用试用接口", notes = "生成一条授权码，并与mac或者主板号进行绑定")
    public MessageOutput trailAuthorization(@RequestBody String body){
        JSONObject bodyJson = JSON.parseObject(body);
        JSONArray macIds = bodyJson.getJSONArray("mac");
        String[] macIdArray = macIds.toArray(new String[macIds.size()]);
        List<String> motherBoardList = Arrays.asList(macIdArray);
        // 1.则创建授权码
        String licenseId = authorizationService.generateAuthorizationCode(TRIAL.getCode());
        // 2.将授权码与物理地址绑定
        authorizationService.bindLicenseAndMac(licenseId, motherBoardList);
        //authorizationService.generateAuthorizationCode(motherBoardList, LicenseEnum.TRIAL.getCode());
        AuthrizationVO authrizationVO = new AuthrizationVO();
        authrizationVO.setExpire(false);
        authrizationVO.setType(TRIAL.getCode());
        return new MessageOutput(new State(CommonConstant.SUCCESS_CODE, "调用试用接口成功"), authrizationVO);
    }
}
